package com.example.modulehome;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.example.baselibs.Constant.HomeSpConstant;
import com.example.baselibs.Constant.UserConstant;
import com.example.baselibs.base.activity.BaseActivity;
import com.example.baselibs.entity.Result;
import com.example.baselibs.entity.bean.UserInfo;
import com.example.baselibs.net.RetrofitManager;
import com.example.baselibs.net.service.UserApi;
import com.example.baselibs.router.path.ModuleHomePath;
import com.example.baselibs.router.path.ModuleMinePath;
import com.example.baselibs.utils.ActivityUtils;
import com.example.baselibs.utils.RequestPermission;
import com.example.baselibs.utils.localsp.SPLocalSave;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

@Route(path = ModuleHomePath.PATH_SPLASH)
public class SplashActivity extends BaseActivity {
    @BindView(R2.id.tv_version)
    TextView tv_version;
    public Boolean isFirstOpen= SPLocalSave.getInstance().getBoolean(HomeSpConstant.IS_FIRST_OPEN);


    @Override
    public int getLayoutRes() {
        Log.i("start","启动Splash页面");
        return R.layout.activity_splash;
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        RequestPermission.requestAllPermission(this);
        //设置版本号
        tv_version.setText(ActivityUtils.getVersion(this));
        Observable.timer(3, TimeUnit.SECONDS)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        //如果用户信息存在，先获取用户信息
                        if(getIsExist()){
                            //获取用户信息
                            loadData();
                        }
                    }

                    @Override
                    public void onNext(Long aLong) {
                        //判断跳转页面
                        //判断是否第一次打开
                        if (isFirstOpen){
                            if (getIsExist()){
                                //跳转Home
                                toActivity(ModuleHomePath.PATH_HOMEACTIVITY);
                            }else {
                                //跳转登录
                                toActivity(ModuleMinePath.PATH_CHECK_UPDATE);

                            }
                        }else {
                            //跳转登录
                            toActivity(ModuleMinePath.PATH_LOGIN);
                            //第一次打开后，标记以后已经不是第一次打开了
                            SPLocalSave.getInstance().putBoolean(HomeSpConstant.IS_FIRST_OPEN,true);
                        }
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private Boolean getIsExist(){
         Boolean isExist=false;
        if (TextUtils.isEmpty(SPLocalSave.getInstance().getString(UserConstant.USER_ID))&&
                TextUtils.isEmpty(SPLocalSave.getInstance().getString(UserConstant.PASSWORD))){
            isExist=true;
            Log.i("bbq","进入");
        }
        return isExist;
    }

    private void loadData() {
        RetrofitManager.getInstance().getUserApiService(UserApi.class).login("(USER_ID='" + SPLocalSave.getInstance().getString(UserConstant.USER_ID)
                + "' and PASSWORD='" + SPLocalSave.getInstance().getString(UserConstant.PASSWORD) + "')")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<UserInfo>>() {
                    @Override
                    public void accept(Result<UserInfo> userInfoResult) throws Exception {
                        if (userInfoResult.isStatus()==true){
                            saveUserInfo(userInfoResult.getData().get(0));
                            return;
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.e(throwable.toString());
                    }
                });

    }

    private void saveUserInfo(final UserInfo use){
        new Thread(){
            @Override
            public void run() {
                SPLocalSave.getInstance().putString(UserConstant.AVARAT, use.getAVATAR());
                SPLocalSave.getInstance().putString(UserConstant.USER_NAME, use.getUSER_NAME());
                SPLocalSave.getInstance().putString(UserConstant.SEX, use.getSEX());
                SPLocalSave.getInstance().putString(UserConstant.BIRTHDAY, use.getBIRTHDAY());
                SPLocalSave.getInstance().putString(UserConstant.QQ, use.getOICQ_NO());
                SPLocalSave.getInstance().putString(UserConstant.EMAIL, use.getEMAIL());
                SPLocalSave.getInstance().putString(UserConstant.ADDRESS, use.getADD_HOME());
                SPLocalSave.getInstance().putInt(UserConstant.UID, use.getUID());
                SPLocalSave.getInstance().putString(UserConstant.USER_PRIV, use.getUSER_PRIV());
                SPLocalSave.getInstance().putString(UserConstant.USER_PRIV_NAME, use.getUSER_PRIV_NAME());
                SPLocalSave.getInstance().putInt(UserConstant.DEPT_ID, use.getDEPT_ID());
                SPLocalSave.getInstance().putInt(UserConstant.JLK_ID, use.getJLK_ID());
                SPLocalSave.getInstance().putString(UserConstant.WORK_NUMBER, use.getTEL_NO_DEPT());
                SPLocalSave.getInstance().putString(UserConstant.PHONE_NUMBER, use.getMOBIL_NO());
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
